package com.mxx.qh.wxapi;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.mxx.qh.R;
import com.mxx.qh.WXAuthorizeBroadCast;
import com.mxx.qh.config.ConstantsConfig;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            api = WXAPIFactory.createWXAPI(this, ConstantsConfig.WX_APP_ID, false);
            Intent intent = getIntent();
            api.handleIntent(intent, this);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq req) {
        Log.i("TAG", "");
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onResp(BaseResp resp) {
        int result;
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = R.string.errcode_success;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.errcode_cancel;
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.errcode_deny;
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                result = R.string.errcode_unsupported;
                break;
            default:
                result = R.string.errcode_unknown;
                break;
        }
        int type = resp.getType();
        Intent mIntent = new Intent();
        mIntent.setAction(WXAuthorizeBroadCast.WX_AUTHORIZE_ACTION);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(ConstantsConfig.TYPE, resp.getType());
        if (type == ConstantsAPI.COMMAND_SENDAUTH) {
            SendAuth.Resp authResp = null;
            if (resp instanceof SendAuth.Resp) {
                authResp = (SendAuth.Resp) resp;
            }
            String code = null;
            String state = null;
            if (authResp != null) {
                code = authResp.code;
                state = authResp.state;
            }
            mBundle.putSerializable(ConstantsConfig.STATE, state);
            mBundle.putSerializable(ConstantsConfig.CODE, code);
        } else {
            mBundle.putSerializable(ConstantsConfig.RESULT, getString(result));
        }
        mIntent.putExtras(mBundle);
        sendBroadcast(mIntent);
    }
}